package com.nbgroup.task;

import com.nbgroup.mapper.StatementMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;

@Slf4j
@Component
public class StatementTask {
    @Autowired
    private StatementMapper statementMapper;

    @Scheduled(cron = "0 0 0 * * ?")
    public void statementTask() {
        log.info("开始往报表写入数据");

        log.info("客户统计表开始");
        LocalDate date1 = LocalDate.now();
        LocalDate date = date1.minusDays(1);
        Integer newClient = 0;
        newClient = statementMapper.selectNewClient(date, date1);
        Integer totalClient = statementMapper.selectTotalClient(date1);
        log.info("{}", totalClient);
        statementMapper.add(date, newClient, totalClient);
        log.info("客户统计表结束");

        log.info("销售额统计表开始");

        Double amount = statementMapper.selectAmount(date, date1);
        if (amount==null){
            amount=0.0;
        }
        log.info("{},{},{}", amount, date, date1);
        statementMapper.addSales(date, amount);
        log.info("销售额统计表结束");


        log.info("线索统计表开始");
        Integer newClue = 0;
        newClue = statementMapper.selectNewClue(date, date1);
        Integer totalClue = statementMapper.selectTotalClue(date1);
        statementMapper.addClue(date, newClue, totalClue);
        log.info("线索统计表结束");


        log.info("线索转化率统计表开始");
        Integer cluesNums = 0;
        //线索池
        Integer clues1 = statementMapper.selectCluesNums1(date1);
        Integer effectiveCluesNums = statementMapper.selectCluesNums2(date, date1); //有效线索数
        cluesNums = clues1 + effectiveCluesNums;
        //商机数
        Integer businessNums = 0;
        businessNums = statementMapper.selectBusinessNums(date, date1);
        Integer contractNums = 0;
        contractNums = statementMapper.selectContractNums(date, date1);
        //往统计表插入数据
        statementMapper.addPercent(date, cluesNums, effectiveCluesNums, businessNums, contractNums);
        log.info("定时任务结束");
    }
}
